<template>
  <div>
    <el-dialog
      :title="title"
      center
      :visible="open"
      :modal="open"
      @close="cancel()"
      width="600px"
    >
      <el-form ref="addForm" :model="addForm" :rules="rules" label-width="80px">
        <el-form-item label="上级组织" prop="parentName">
          <el-input v-model="parentName"  disabled/>
        </el-form-item>
        <el-form-item label="迁移到" prop="treeDeptIds">
          <ht-dept-select-tree
            v-model="treeDeptIds"
            :radio="true"
            :tenantSelect="false"
            :tenantCode="funcParams.tenantCode"
            :disabledCode="addForm.deptCode"
            node-key="code"
            @otherChangeChecked="treeChangeChecked"
          ></ht-dept-select-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  findDeptInfoById
} from "@/api/authority_app/deptInfo/deptInfo";
export default {
  name: "deptMove",
  components: {  },
  data() {
    let checkParent = (rule,value,callback) => {
      if(!this.treeDeptIds || this.treeDeptIds.length < 1) {
        callback(new Error('请选择上级组织'));
      } else {
        callback();
      }
    };
    return {
      treeDeptIds:[],
      checkedDeptIds: '',
      parentName: '',
      rules: {
        treeDeptIds: [
          { type: 'array', required: true,message: "请选择上级组织", trigger: "change", validator: checkParent},
        ],
      },
    };
  },
  props: {
    title: {
      type: String,
      default: "",
    },
    open: {
      type: Boolean,
      default: false,
    },
    funcParams: {
      type: Object,
      default: {
        deptCode: "",
        tenantCode: "",
        name: "",
        userId: "",
        deptId: "",
      },
    },
    addForm: {
      id: "",
      parentId: "",
      deptCode: "",
      deptName: "",
      orderNum: "",
      leader: "",
      phone: "",
      email: ""
    },
    checkMenu: {
      type: Array,
      default: [],
    },
    statusOptions: {
      type: Array,
      default: [],
    },
    deptInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  created() {
    // this.queryFormDeptTreeReq();
    this.treeDeptIds = this.addForm.parentCode;
    this.checkedDeptIds = this.addForm.parentId;
    // if(this.addForm.id != null && this.addForm.id != ''){
    //   selectDeptNameById(this.addForm.id).then(response => {
    //     this.parentName = response.result;
    //   });
    // }
  },
  mounted() {
    if(this.addForm.id) {
      // 查询机构详情
      findDeptInfoById(this.addForm.id).then(res => {
        this.deptInfo = res.result
        this.parentName = res.result.parentName
      })
    }
  },
  methods: {
    //新增\编辑提交接口
    addOrUpdate() {
      this.addForm.parentId = this.checkedDeptIds//.join(",");
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          this.$emit("submitAddForm", this.addForm);
        }
      });
    },
    treeChangeChecked(res) {
      this.checkedDeptIds = res.map(x=>x.id).join(',');
    },
    //取消接口
    cancel() {
      this.$emit("changeOpen", false);
    },
  },
};
</script>
